#include<bits/stdc++.h>
using namespace std;
namespace IO{
    const int BUF=1<<21;
    char buff[BUF],*p1=buff,*p2=buff;
    #define gc() (p1==p2&&(p2=((p1=buff)+fread(buff,1,BUF,stdin)),p1==p2)?EOF:*p1++)
    template<typename T>inline void read(T &x){
        char ch=gc();x=0;int f=1;
        while(!isdigit(ch)){if(ch=='-')f=-1;ch=gc();}
        while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=gc();
        x*=f;
    }
    char obuf[BUF],*p3=obuf;
    inline void pc(char ch){
        if(p3-obuf<BUF) *p3++=ch;
        else fwrite(obuf,p3-obuf,1,stdout),p3=obuf,*p3++=ch;
    }
    inline void put(const char *s){while(*s) pc(*s),++s;}
    char ch[32];int ctop;
    template<typename T>inline void print(T x){
    	if(x<0) x=~x+1,pc('-');
        if(!x) return pc(48);
        while(x) ch[++ctop]=x%10^48,x/=10;
        while(ctop) pc(ch[ctop--]);
    }
    inline void flush(){fwrite(obuf,p3-obuf,1,stdout);p3=obuf;}
}
using namespace IO;
const int N=5e5+5;
int S,n,X1=N,X2,sz[N];
#define ll long long
ll ans;
vector<int> V[N],id[N],G[N];
struct node{int x,l,r;}a[N];
vector<int> f[N],g[N],sf[N],sg[N];
void dfs1(int p,int fa){
	int len=a[p].r-a[p].l+1;
	sz[p]=len;
	f[p].resize(len);
	g[p].resize(len);
	for(int v:G[p]) if(v^fa){
		//cerr<<p<<' '<<a[p].l<<' '<<a[p].r<<' '<<v<<' '<<a[v].l<<' '<<a[v].r<<'\n';
		dfs1(v,p);
		for(int i=a[v].l;i<=a[v].r;++i) f[p][max(min(i,a[p].r),a[p].l)-a[p].l]+=f[v][i-a[v].l];
	
		ans+=1ll*sf[v].back()*(S-sz[v]);
	
		sz[p]+=sz[v];
	}
	for(int i=0;i<len;++i) ++f[p][i];//,printf("f[%d][%d]=%d ",p,i,f[p][i]);cerr<<'\n';
	sf[p].resize(len);
	for(int i=0;i<len;++i) sf[p][i]=(i?sf[p][i-1]:0)+f[p][i];
}
void dfs2(int p,int fa){
	int len=a[p].r-a[p].l+1;
	sg[p].resize(len);
	int now=ans;
	for(int i=0;i<len;++i) sg[p][i]=(i?sg[p][i-1]:0)+g[p][i];//,printf("g[%d][%d]=%d ",p,i,g[p][i]);cerr<<'\n';
	for(int i=0;i<len-1;++i)
		ans+=1ll*(sf[p][i]+sg[p][i])*(f[p][i+1]+g[p][i+1]);
	for(int v:G[p]){
		for(int i=max(a[p].l,a[v].l);i<min(a[p].r,a[v].r);++i){
			if(v==fa) ans-=1ll*(sf[p][i-a[p].l]+sg[p][i-a[p].l])*g[v][i+1-a[v].l];//,cerr<<"U"<<1ll*sg[v][i-a[v].l]*(sg[v].back()-sg[v][i-a[v].l])<<'\n';
			else ans-=1ll*(sf[p][i-a[p].l]+sg[p][i-a[p].l])*f[v][i+1-a[v].l];//,cerr<<"D"<<1ll*sf[v][i-a[v].l]*(sf[v].back()-sf[v][i-a[v].l])<<'\n';
		}
	}//cerr<<"G"<<a[p].l<<' '<<a[p].r<<' '<<ans-now<<'\n';
	for(int v:G[p]) if(v^fa){
		/////////////
		for(int i=a[p].l;i<=a[p].r;++i) g[v][min(max(i,a[v].l),a[v].r)-a[v].l]+=g[p][i-a[p].l]+f[p][i-a[p].l];
		for(int i=max(a[p].l,a[v].l);i<=min(a[p].r,a[v].r);++i) g[v][i-a[v].l]-=f[v][i-a[v].l];
		dfs2(v,p);
	}
}
int main(){
//	freopen("game3.in","r",stdin);
	read(n);
	S=n;
	while(n--){
		int x,y;
		read(x),read(y);
		X1=min(X1,x);
		X2=max(X2,x);
		V[x].push_back(y);
	}
	n=0;
	for(int i=X1;i<=X2;++i){
		sort(V[i].begin(),V[i].end());
		int p=0;
		for(int j=0;j<(int)V[i].size();++j){
			int k=j;
			while(k+1<(int)V[i].size()&&V[i][k+1]==V[i][k]+1) ++k;
			a[++n]={i,V[i][j],V[i][k]};
			//cerr<<"X"<<i<<' '<<a[n].l<<' '<<a[n].r<<' '<<n<<'\n';
			id[i].push_back(n);
			while(p<(int)id[i-1].size()&&a[id[i-1][p]].r<a[n].l) ++p;
			for(int k=p;k<(int)id[i-1].size()&&a[id[i-1][k]].l<=a[n].r;++k){
				p=k;
				//cerr<<"add"<<n<<' '<<id[i-1][k]<<' '<<k<<'\n';
				G[n].push_back(id[i-1][k]);
				G[id[i-1][k]].push_back(n);
			}
			j=k;
		}
	}
	dfs1(1,0);
	dfs2(1,0);
	cout<<ans*2;
	return 0;
}


